package leetcode.editor.cn;

import java.util.LinkedList;
import java.util.Queue;

public class _101_SymmetricTree {
    public static void main(String[] args) {
        Solution solution = new _101_SymmetricTree().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution1 {

        private boolean isSymmetric(TreeNode left, TreeNode right) {
            if (left == null && right == null) {
                return true;
            }
            if (left == null || right != null) {
                return false;
            }
            return left.val == right.val && isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
        }


        public boolean isSymmetric(TreeNode root) {
            if (root == null) {
                return true;
            }
            return isSymmetric(root.left, root.right);
        }
    }

    class Solution {

        public boolean isSymmetric(TreeNode root) {
            if (root == null) {
                return true;
            }
            Queue<TreeNode> q = new LinkedList<>();
            q.add(root.left);
            q.add(root.right);
            while (!q.isEmpty()) {
                TreeNode u = q.peek();
                q.remove();
                TreeNode v = q.peek();
                q.remove();

                if (u == null && v == null) {
                    continue;
                }
                if (u == null || v == null || u.val != v.val) {
                    return false;
                }

                q.add(u.left);
                q.add(v.right);

                q.add(u.right);
                q.add(v.left);
            }
            return true;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}